home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / borcmpad / mpopen.c < prev    next >
C/C++ Source or Header  |  1991-07-23  |  9KB  |  274 lines

  1. /***************************************************************************
  2.  *                                       *
  3.  *  MODULE    : MpOpen.c                           *
  4.  *                                       *
  5.  *  PURPOSE    : Contains the file open dialog function and it's helper   *
  6.  *          functions.                           *
  7.  *                                       *
  8.  *  FUNCTIONS    : IsWild ()          - Ascertains that the input string   *
  9.  *                    contains a DOS wildcard character. *
  10.  *                                       *
  11.  *          SelectFile()          - If filename supplied contains a    *
  12.  *                    wildcard, this function fills the  *
  13.  *                    listboxes in File/Open dialog, else*
  14.  *                    the dialog is closed.           *
  15.  *                                       *
  16.  *          FileOpenDlgProc()   - Dialog funcion for the File/Open   *
  17.  *                    dialog.                *
  18.  *                                       *
  19.  *          GetFileName ()      - Gets a file name from the user.    *
  20.  *                                       *
  21.  ***************************************************************************/
  22. #include "multipad.h"
  23.  
  24. char szPropertyName [] = "FILENAME";/* Name of the File name property list item */
  25.  
  26. int FAR PASCAL DialogBoxParam ( HANDLE, LPSTR, HWND, FARPROC, LONG);
  27.  
  28.  
  29. /****************************************************************************
  30.  *                                        *
  31.  *  FUNCTION   : IsWild ( psz )                         *
  32.  *                                        *
  33.  *  PURPOSE    : Checks if the string (referenced by a NEAR pointer)        *
  34.  *         contains a DOS wildcard character ("*" or "?").        *
  35.  *                                        *
  36.  *  RETURNS    : TRUE  - iff the string contains a wildcard character.        *
  37.  *         FALSE - otherwise.                     .        *
  38.  *                                        *
  39.  ****************************************************************************/
  40. BOOL NEAR PASCAL IsWild( psz )
  41. register PSTR psz;
  42. {
  43.     for(;;)
  44.     switch (*psz++){
  45.         case '*':
  46.         case '?':
  47.         /* Found wildcard */
  48.         return TRUE;
  49.  
  50.         case 0:
  51.         /* Reached end of string */
  52.         return FALSE;
  53.  
  54.         default:
  55.         continue;
  56.     }
  57. }
  58.  
  59. /****************************************************************************
  60.  *                                        *
  61.  *  FUNCTION   : FileExists(pch)                                            *
  62.  *                                        *
  63.  *  PURPOSE    : Checks to see if a file exists with the path/filename        *
  64.  *               described by the string pointed to by 'pch'.               *
  65.  *                                        *
  66.  *  RETURNS    : TRUE  - if the described file does exist.                  *
  67.  *               FALSE - otherwise.                                         *
  68.  *                                        *
  69.  ****************************************************************************/
  70.  
  71. BOOL FileExists(PSTR pch)
  72. {
  73.     int fh;
  74.  
  75.     if ((fh = _lopen((LPSTR) pch, 0)) < 0)
  76.          return(FALSE);
  77.  
  78.     _lclose(fh);
  79.     return(TRUE);
  80. }
  81.  
  82. /****************************************************************************
  83.  *                                        *
  84.  *  FUNCTION   : SelectFile ( hwnd )                        *
  85.  *                                        *
  86.  *  PURPOSE    : Reads the string in the edit control of the File/Open        *
  87.  *         dialog. If it contains a wildcard, then it attempts to     *
  88.  *         fill the listboxes in the File/Open dialog. Othewise it    *
  89.  *         ends the dialog. Modifies the FILENAME item in the property*
  90.  *         list of the window.                        *
  91.  *                                        *
  92.  ****************************************************************************/
  93.  
  94. VOID NEAR PASCAL SelectFile( hwnd )
  95.  
  96. register HWND hwnd;
  97. {
  98.     register PSTR pch;
  99.     PSTR      pch2;
  100.  
  101.     /* Get handle (near address) to filename data in window's property list */
  102.     pch = (PSTR)GetProp (hwnd, PROP_FILENAME);
  103.  
  104.     /* Get the text from the dialog's edit control into this address */
  105.     GetDlgItemText (hwnd, IDD_FILENAME, pch, 64);
  106.  
  107.     if (IsWild(pch)){
  108.     /* Select the directory and make a listing of the directories */
  109.     DlgDirList(hwnd, (LPSTR)pch, IDD_DIRS, IDD_PATH, ATTR_DIRS);
  110.  
  111.     /* Obtain the filename-only part of the path in the edit control */
  112.     for (pch2 = pch; *pch; pch++)
  113.         if ((*pch == '\\') || (*pch == ':'))
  114.         pch2 = pch + 1;
  115.  
  116.     /* List the files in this directory based on the wildcard. */
  117.     DlgDirList(hwnd, (LPSTR)pch2, IDD_FILES, IDD_PATH, ATTR_FILES);
  118.  
  119.     /* Set the dialog's edit control to the filename part of path
  120.      * string.
  121.      */
  122.     SetDlgItemText (hwnd, IDD_FILENAME, pch2);
  123.     }
  124.     else
  125.     {
  126.     /* The filename in the property list is not a wildcard */
  127.     if (FileExists(pch)){
  128.  
  129.         RemoveProp (hwnd, PROP_FILENAME);
  130.         EndDialog (hwnd, 0);
  131.     }
  132.     else{
  133.         MPError ( hwnd, MB_OK | MB_SYSTEMMODAL, IDS_CANTOPEN, (LPSTR) pch);
  134.         SetActiveWindow (hwnd);
  135.     }
  136.     }
  137. }
  138. /****************************************************************************
  139.  *                                        *
  140.  *  FUNCTION   : FileOpenDlgProc()                        *
  141.  *                                        *
  142.  *  PURPOSE    : Dialog function for the File/Open dialog. Takes care of    *
  143.  *         calling the appropriate functions for extracting the        *
  144.  *         filename and wildcard, filling the listboxes and changing  *
  145.  *         the FILENAME item in the property list for the window.     *
  146.  *                                        *
  147.  ****************************************************************************/
  148.  
  149. BOOL FAR PASCAL FileOpenDlgProc ( hwnd, message, wParam, lParam)
  150. register HWND hwnd;
  151. WORD          message;
  152. register WORD wParam;
  153. LONG          lParam;
  154. {
  155.     PSTR pch;
  156.  
  157.     switch (message){
  158.  
  159.     case WM_INITDIALOG:
  160.         /* Set the default file extension on edit window, and try to
  161.          * get a listing of the files and directories.
  162.          */
  163.         SetDlgItemText ( hwnd, IDD_FILENAME, DEFFILESEARCH);
  164.         SetProp (hwnd, PROP_FILENAME, LOWORD(lParam));
  165.         SendDlgItemMessage (hwnd, IDD_FILENAME, EM_LIMITTEXT, 64, 0L);
  166.         SelectFile (hwnd);
  167.         break;
  168.  
  169.     case WM_COMMAND:
  170.         switch (wParam){
  171.         case IDOK:
  172.             SelectFile(hwnd);
  173.             break;
  174.  
  175.         case IDCANCEL:
  176.             /* Set the filename in the prop. list to NULL and quit */
  177.             pch  = (PSTR) GetProp (hwnd, PROP_FILENAME);
  178.             *pch = 0;
  179.             EndDialog (hwnd, 0);
  180.             break;
  181.  
  182.         case IDD_FILENAME:
  183.             /* Enable the OK button if the edit control has text. */
  184.             EnableWindow ( GetDlgItem (hwnd, IDOK),
  185.                    GetWindowTextLength ((HWND)LOWORD (lParam)));
  186.             break;
  187.  
  188.         case IDD_FILES:
  189.  
  190.             /* The files listbox. If file selection has changed, fill
  191.              * the new filename into the property list buffer and set
  192.              * text in edit control.
  193.              */
  194.             if (HIWORD(lParam) == LBN_SELCHANGE){
  195.             pch = (PSTR) GetProp (hwnd, PROP_FILENAME);
  196.             DlgDirSelect (hwnd, (LPSTR)pch, IDD_FILES);
  197.             SetDlgItemText (hwnd, IDD_FILENAME, (LPSTR)pch);
  198.             }
  199.             else if (HIWORD(lParam) == LBN_DBLCLK)
  200.             /* if the item was double-clicked, try to open it */
  201.             SelectFile(hwnd);
  202.             break;
  203.  
  204.         case IDD_DIRS:
  205.  
  206.             /* The directories listbox. Append current filename in edit
  207.              * control (stripped of the path prefix) to the name from
  208.              * the property list and set the new string in the edit
  209.              * control.
  210.              */
  211.             if (HIWORD(lParam) == LBN_SELCHANGE){
  212.  
  213.             PSTR pch2, pchT, pchS;
  214.  
  215.             pch = (PSTR) GetProp (hwnd, PROP_FILENAME);
  216.  
  217.             /* Get the new drive/dir */
  218.             DlgDirSelect (hwnd, pch, IDD_DIRS);
  219.             pch2 = pch + lstrlen(pch);
  220.  
  221.             /* Fetch current contents of dialog's edit control and append
  222.              * it to name from property list... */
  223.             GetDlgItemText(hwnd,IDD_FILENAME,(LPSTR)pch2,64);
  224.             if (*pch2 == 0){
  225.                 SetDlgItemText(hwnd, IDD_FILENAME, DEFFILESEARCH);
  226.                 GetDlgItemText(hwnd,IDD_FILENAME,(LPSTR)pch2,64);
  227.             }
  228.             else {
  229.                 pchS = pch;
  230.                 for (pchT = pch = pch2; *pch; pch++) {
  231.                 if (*pch == '\\' || *pch == ':')
  232.                     pchT = pch2;
  233.                 else
  234.                     *pchT++ = *pch;
  235.                 }
  236.                 *pchT = 0;
  237.                 pch = pchS;
  238.             }
  239.  
  240.             /* Set the edit control with new string */
  241.             SetDlgItemText (hwnd, IDD_FILENAME, (LPSTR)pch);
  242.             }
  243.             else if (HIWORD(lParam) == LBN_DBLCLK)
  244.             SelectFile (hwnd);
  245.             break;
  246.  
  247.         default:
  248.             return FALSE;
  249.         }
  250.         break;
  251.  
  252.     default:
  253.         return FALSE;
  254.     }
  255.     return TRUE;
  256. }
  257.  
  258. /****************************************************************************
  259.  *                                        *
  260.  *  FUNCTION   : GetFilename ( pstr )                        *
  261.  *                                        *
  262.  *  PURPOSE    : Gets a filename from the user by calling the File/Open     *
  263.  *         dialog.                            *
  264.  *                                        *
  265.  ****************************************************************************/
  266. VOID NEAR PASCAL GetFileName(PSTR pstr)
  267. {
  268.     FARPROC lpfn;
  269.  
  270.     lpfn = MakeProcInstance (FileOpenDlgProc, hInst);
  271.     DialogBoxParam (hInst, IDD_FILEOPEN, hwndFrame, lpfn, MAKELONG(pstr,0));
  272.     FreeProcInstance (lpfn);
  273. }
  274.